package com.ytjj.qmyx.admin.controller.imController;

import com.alibaba.excel.util.StringUtils;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.constants.im.ImRedisKeyConstant;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.admin.model.request.im.ImSessionInfoRequest;
import com.ytjj.qmyx.admin.model.vo.ImSessionInfoVO;
import com.ytjj.qmyx.admin.service.imService.ImServiceUserService;
import com.ytjj.qmyx.admin.service.imService.ImSessionInfoService;
import com.ytjj.qmyx.admin.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 会话管理controller
 */
@RestController
@RequestMapping("/imSession")
@Slf4j
public class ImSessionInfoController {
    @Autowired
    private ImSessionInfoService imSessionInfoService;
    @Autowired
    private ImServiceUserService imServiceUserService;
    @Resource
    private RedisService redisService;

    /**
     * 客服结束会话
     * 参数:sessionId
     * @param request
     * @return
     */
    @PostMapping("/finshSession")
    public CommonResult finishSession(@RequestBody ImSessionInfoRequest request) {
        return imSessionInfoService.finalSession(request);
    }

    /**
     * 获取该客服的客户列表
     * @param
     * @return
     */
    @GetMapping("/getCustomerList")
    public CommonResult getCustomerList(@RequestParam(required = false) Integer sessionId){
        String userId = UserUtil.getPrincipal();
        try {
            imServiceUserService.insertOrUpdate(userId);
            //记录客服当日首次登录时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String curDate = sdf.format(new Date());

            String im_login_time = redisService.get(ImRedisKeyConstant.IM_LOGIN_TIME + curDate + ":" + userId);
            if (StringUtils.isEmpty(im_login_time)) {
                redisService.set(ImRedisKeyConstant.IM_LOGIN_TIME + curDate + ":" + userId, String.valueOf(System.currentTimeMillis()));
                redisService.expire(ImRedisKeyConstant.IM_LOGIN_TIME + curDate + ":" + userId, 18 * 60 * 60);
            }
        } catch (Exception e) {
            log.error("===客服上线计时异常==={}", userId);
        }
        return CommonResult.success(imSessionInfoService.getCustomerList(userId,sessionId));
    }

    /**
     *   根据客户唯一标识获取访问记录
     * @param imSessionInfoVO
     * @return
     */
    @PostMapping("/getAccessRecord")
    public CommonResult getAccessRecord(@RequestBody ImSessionInfoVO imSessionInfoVO){
        return CommonResult.success(imSessionInfoService.getAccessRecord(imSessionInfoVO.getCustomerId(),imSessionInfoVO.getId()));
    }

    /**
     *   条件获取访问记录
     * @param imSessionInfoVO
     * @return
     */
    @PostMapping("/getAllAccessRecord")
    public CommonResult getAllAccessRecord(@RequestBody ImSessionInfoVO imSessionInfoVO){
        if (null == imSessionInfoVO.getId()
                && StringUtils.isEmpty(imSessionInfoVO.getCustomerId())
                && StringUtils.isEmpty(imSessionInfoVO.getChannel())
                && StringUtils.isEmpty(imSessionInfoVO.getServiceId())
                && null == imSessionInfoVO.getStartTime()
                && null == imSessionInfoVO.getEndTime()
                && StringUtils.isEmpty(imSessionInfoVO.getUserName())) {
            return CommonResult.success();
        }
        return CommonResult.success(imSessionInfoService.getAllAccessRecord(imSessionInfoVO));
    }

    /**
     *  更新客户满意度
     * @return
     */
    @PostMapping("/updateSatisfaction")
    public CommonResult updateSatisfaction(@RequestBody ImSessionInfoVO imSessionInfoVO){
        imSessionInfoService.updateSatisfaction(imSessionInfoVO);
        return CommonResult.success();
    }

    /**
     * 查询所有客服名称
     */
    @GetMapping("/selectName")
    public CommonResult selectName(@RequestParam(required = false) Integer type){
        return CommonResult.success(imSessionInfoService.selectName(type));
    }

    /**
     * 选择客户重新发现会话
     * @param customerId
     * @return
     */
    @PostMapping("/updateSessionByCustomerId")
    public CommonResult updateSessionByCustomerId(String customerId,Integer leaveMsgId){
        try {
            String userId = UserUtil.getPrincipal();
            imSessionInfoService.updateSessionByCustomerId(customerId,userId,leaveMsgId);
        } catch (Exception e) {
            log.error("重新发起会话接口失败,{}",e.getMessage());
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success();
    }

}
